package com.example.stringbufferlogger.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.stringbufferlogger.entity.FileDataRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface FileDataRecordMapper extends BaseMapper<FileDataRecord> {

    @Select("SELECT * FROM FILE_DATA_RECORD WHERE request_id = #{requestId} AND is_compare = 0")
    List<FileDataRecord> selectByRequestId(String requestId);

    /**
     * 通过 requestId 查询 interfaceUrl
     *
     * @param requestId 请求 ID
     * @return 对应的 interfaceUrl，如果未找到则返回 null
     */
    @Select("SELECT interface_url FROM FILE_DATA_RECORD WHERE request_id = #{requestId} AND ROWNUM = 1")
    String getInterfaceUrlByRequestId(String requestId);

    /**
     * 通过 requestId 查询 interfaceUrl
     *
     * @param interface_url 请求 ID
     * @return 对应的 interfaceUrl，如果未找到则返回 null
     */
    @Select("SELECT requestId FROM FILE_DATA_RECORD WHERE interface_url = #{interface_url} AND ROWNUM = 1")
    String getRequestIdByInterfaceUrl(String interface_url);

    /**
     * 根据 requestId 筛选不匹配的数据
     * @param requestId 请求 ID
     * @return 不匹配的数据列表
     */
    @Select("SELECT a.* FROM file_data_record a WHERE a.is_compare <> 1 AND a.record_type = 'new' " +
            "AND NOT EXISTS (SELECT 1 FROM file_data_record b WHERE b.is_compare <> 1 " +
            "AND b.record_type = 'old' AND a.request_id = #{requestId} " +
            "AND a.md5_code = b.md5_code) " +
            "UNION ALL " +
            "SELECT a.* FROM file_data_record a WHERE a.is_compare <> 1 AND a.record_type = 'old' " +
            "AND NOT EXISTS (SELECT 1 FROM file_data_record b WHERE b.is_compare <> 1 " +
            "AND b.record_type = 'new' AND a.request_id = #{requestId} " +
            "AND a.md5_code = b.md5_code)")
    List<FileDataRecord> selectUnmatchedRecords(@Param("requestId") String requestId);

    /**
     * 更新不匹配数据的 IsMatch 和 IsCompare 字段
     * @param requestId 请求 ID
     */
    @Update("UPDATE file_data_record a " +
            "SET a.is_match = 0, a.is_compare = 1 " +
            "WHERE a.is_compare <> 1 AND a.request_id = #{requestId} " +
            "AND ((a.record_type = 'new' AND NOT EXISTS (SELECT 1 FROM file_data_record b WHERE b.is_compare <> 1 AND b.record_type = 'old' AND a.request_id = b.request_id AND a.md5_code = b.md5_code)) " +
            "OR (a.record_type = 'old' AND NOT EXISTS (SELECT 1 FROM file_data_record b WHERE b.is_compare <> 1 AND b.record_type = 'new' AND a.request_id = b.request_id AND a.md5_code = b.md5_code)))")
    void updateUnmatchedRecords(@Param("requestId") String requestId);

    /**
     * 更新匹配数据的 IsMatch 和 IsCompare 字段
     * @param requestId 请求 ID
     */
    @Update("UPDATE file_data_record a " +
            "SET a.is_match = 1, a.is_compare = 1 " +
            "WHERE a.is_compare <> 1 AND a.request_id = #{requestId} " +
            "AND NOT ((a.record_type = 'new' AND NOT EXISTS (SELECT 1 FROM file_data_record b WHERE b.is_compare <> 1 AND b.record_type = 'old' AND a.request_id = b.request_id AND a.md5_code = b.md5_code)) " +
            "OR (a.record_type = 'old' AND NOT EXISTS (SELECT 1 FROM file_data_record b WHERE b.is_compare <> 1 AND b.record_type = 'new' AND a.request_id = b.request_id AND a.md5_code = b.md5_code)))")
    void updateMatchedRecords(@Param("requestId") String requestId);


}